<?php

class phpIPTC {

    var $file;
    var $background = '000000';
    var $color = 'FFFFFF';
    var $width;
    var $height = 20;

    function phpIPTC ($arr = array ()) {
        $this->set ($arr);
    }

    function set ($arr) {
        if (is_array ($arr) && !empty ($arr)) {
            foreach ($arr as $key => $value) {
                if (in_array ($key, array_keys (get_class_vars (get_class ($this))))) {
                    $this->$key = $arr[$key];
                }
            }
        }
    }

    function makeImage ($input) {
        if (is_writable ($this->file)) {
            $im = imagecreatetruecolor (!empty ($this->width) ? $this->width : (strlen ($input) * 8) + 4, $this->height);
            imagefill ($im, 0, 0, $this->allocateFromHex ($im, $this->background));
            if (!is_null ($input)) {
                imagestring ($im, 4, 2, 2, $input, $this->allocateFromHex ($im, $this->color));
            }
            imagejpeg ($im, $this->file, 80);
            imagedestroy ($im);
        }
    }

    function allocateFromHex ($im, $hexstr) {
        $int = hexdec ($hexstr);
        return (imagecolorallocate ($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int));
    }

}

class phpIptcTags {

    var $file;

    function phpIptcTags ($file) {
        if (is_file ($file)) {
            $this->file = $file;
            return ($this);
        }
        return (FALSE);
    }

    function read ($tag = null) {
        if (is_readable ($this->file)) {
            GetImageSize ($this->file, &$info);
            if (isset ($info["APP13"])) {
                $this->iptc = iptcparse ($info["APP13"]);
            }
        }
        return (!is_null ($tag) ? $this->iptc["2#" . $tag][0]:$this->iptc);
    }

    function set ($tag, $input) {
        if (!empty ($tag) && !empty($input) && isset ($this->iptc)) {
            $this->iptc["2#" . $tag][0] = $input;
        }
    }

    function write () {
        if (isset ($this->iptc) && is_writable ($this->file)) {
            $tags = '';
            foreach (array_keys ($this->iptc) as $s){
                $tags .= $this->tag (2, str_replace ("2#", "", $s), $this->iptc[$s][0]);
            }
            $content = iptcembed ($tags, $this->file, 0);
            $fp = fopen ($this->file, "w");
            fwrite ($fp, $content);
            fclose ($fp);
        }
    }
    
    // Function to format the new IPTC text, (thanks to Thies C. Arntzen)
    function tag ($rec, $dat, $val) {
        $len = strlen ($val);
        if ($len < 0x8000) {
            return (
                chr (0x1c).chr($rec).chr($dat) .
                chr ($len >> 8) .
                chr ($len & 0xff) .
                $val
            );
        }
        return (
            chr (0x1c).chr($rec).chr($dat) .
            chr (0x80).chr(0x04) .
            chr (($len >> 24) & 0xff) .
            chr (($len >> 16) & 0xff) .
            chr (($len >> 8 ) & 0xff) .
            chr (($len ) & 0xff).
            $val
        );
    }

}

class phpIPTCcounter extends phpIPTC {

    var $value;

    function get () {
        $iptc = new phpIptcTags ($this->file);
        $this->value = $iptc->read (120) + 1;
        $this->makeImage ($this->width * $this->height == 1 ? null:$this->value);
        $iptc->set (120, $this->value);
        $iptc->write ();
        return ($this->file);
    }

}

?>
